package domain.entities;// default package

import java.util.List;
import java.util.Set;
import org.hibernate.LockMode;
import org.hibernate.criterion.Restrictions;
import org.hibernate.hql.ast.tree.RestrictableStatement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


/**
 * A data access object (DAO) providing persistence and search support for
 * Countries entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see .Countries
 * @author MyEclipse Persistence Tools
 */

public class CountriesDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(CountriesDAO.class);
	// property constants
	public static final String COUNTRY = "country";
	public static final String FIPS104 = "fips104";
	public static final String ISO2 = "iso2";
	public static final String ISO3 = "iso3";
	public static final String ISON = "ison";
	public static final String INTERNET = "internet";
	public static final String CAPITAL = "capital";
	public static final String MAP_REFERENCE = "mapReference";
	public static final String NATIONALITY_SINGULAR = "nationalitySingular";
	public static final String NATIONALITY_PLURAL = "nationalityPlural";
	public static final String CURRENCY = "currency";
	public static final String CURRENCY_CODE = "currencyCode";
	public static final String POPULATION = "population";
	public static final String TITLE = "title";
	public static final String COMMENT = "comment";

	protected void initDao() {
		// do nothing
	}

	public void save(Countries transientInstance) {
		log.debug("saving Countries instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Countries persistentInstance) {
		log.debug("deleting Countries instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

/*	public Countries findById(Short id) {
		log.debug("getting Countries instance with id: " + id);
		try {
			Countries instance = (Countries) getHibernateTemplate().get(
					"Countries", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}*/

    public Countries findById(Short id) {
	    List<Countries> result = getSession().createCriteria(Countries.class)
                .add(Restrictions.eq("countryId", id))
                .list();
        if(result.size() > 0)
            return result.get(0);
        return null;
	}


	public List<Countries> findByExample(Countries instance) {
		log.debug("finding Countries instance by example");
		try {
			List<Countries> results = (List<Countries>) getHibernateTemplate()
					.findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Countries instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Countries as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Countries> findByCountry(Object country) {
		return findByProperty(COUNTRY, country);
	}

	public List<Countries> findByFips104(Object fips104) {
		return findByProperty(FIPS104, fips104);
	}

	public List<Countries> findByIso2(Object iso2) {
		return findByProperty(ISO2, iso2);
	}

	public List<Countries> findByIso3(Object iso3) {
		return findByProperty(ISO3, iso3);
	}

	public List<Countries> findByIson(Object ison) {
		return findByProperty(ISON, ison);
	}

	public List<Countries> findByInternet(Object internet) {
		return findByProperty(INTERNET, internet);
	}

	public List<Countries> findByCapital(Object capital) {
		return findByProperty(CAPITAL, capital);
	}

	public List<Countries> findByMapReference(Object mapReference) {
		return findByProperty(MAP_REFERENCE, mapReference);
	}

	public List<Countries> findByNationalitySingular(Object nationalitySingular) {
		return findByProperty(NATIONALITY_SINGULAR, nationalitySingular);
	}

	public List<Countries> findByNationalityPlural(Object nationalityPlural) {
		return findByProperty(NATIONALITY_PLURAL, nationalityPlural);
	}

	public List<Countries> findByCurrency(Object currency) {
		return findByProperty(CURRENCY, currency);
	}

	public List<Countries> findByCurrencyCode(Object currencyCode) {
		return findByProperty(CURRENCY_CODE, currencyCode);
	}

	public List<Countries> findByPopulation(Object population) {
		return findByProperty(POPULATION, population);
	}

	public List<Countries> findByTitle(Object title) {
		return findByProperty(TITLE, title);
	}

	public List<Countries> findByComment(Object comment) {
		return findByProperty(COMMENT, comment);
	}

    public List findAll() {
		log.debug("finding all Countries instances");
		try {
			String queryString = "from Countries";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

    public Countries merge(Countries detachedInstance) {
		log.debug("merging Countries instance");
		try {
			Countries result = (Countries) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

    public void attachDirty(Countries instance) {
		log.debug("attaching dirty Countries instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Countries instance) {
		log.debug("attaching clean Countries instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static CountriesDAO getFromApplicationContext(ApplicationContext ctx) {
		return (CountriesDAO) ctx.getBean("CountriesDAO");
	}
}